package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * 门店信息
 */
@TableBind(tableName="site",pkName = "id")
public class Site extends BasicModel<Site> {
    public static final Site dao = new Site();
    public static final String SELECT_ALL = "`id`,`name`,`address`,`contact`,`contact_phone`,`email`,`delivery_address`,`remark`,`user_id`,`distributor_id`";

    public List<Site> findByDistributor(Long distributorId) {
        return find("select * from site where distributor_id = ?", distributorId);
    }

    @Override
    public Page<Site> paginate(Integer pageNumber, Integer pageSize, String column, Site model) {
        List<Object> paras = new ArrayList<>();
        StringBuilder sqlExceptSelectBuffer = new StringBuilder("FROM " + getTableName() + " WHERE 1=1");
        StringBuilder selectBuffer = new StringBuilder("SELECT ").append(column);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        } else {
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (model != null) {
            String[] attrNames = model.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = model.get(attrName);
                if (attrValue != null) {
                    if(attrName.equals("name")){
                        sqlExceptSelectBuffer.append(" AND `name` LIKE ? ");
                        attrValue = "%"+attrValue+"%";
                    }else if(attrName.equals("address")){
                        sqlExceptSelectBuffer.append(" AND `address` LIKE ? ");
                        attrValue = "%"+attrValue+"%";
                    }else if(attrName.equals("contact")){
                        sqlExceptSelectBuffer.append(" AND `contact` LIKE ? ");
                        attrValue = "%"+attrValue+"%";
                    }else if(attrName.equals("contact_phone")){
                        sqlExceptSelectBuffer.append(" AND `contact_phone` LIKE ? ");
                        attrValue = "%"+attrValue+"%";
                    }else if(attrName.equals("delivery_address")){
                        sqlExceptSelectBuffer.append(" AND `delivery_address` LIKE ? ");
                        attrValue = "%"+attrValue+"%";
                    }else{
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }

        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }
            
    public Site setId(Long id){
        return this.set("id",id);
    }

    public Long getId(){
        return this.get("id");
    }

    public Site setDistributorId(Long distributorId){
        return this.set("id",distributorId);
    }

    public Long getDistributorId(){
        return this.get("distributor_id");
    }
        
    public Site setName(String name){
        return this.set("name",name);
    }

    public String getName(){
        return this.getStr("name");
    }
        
    public Site setAddress(String address){
        return this.set("address",address);
    }

    public String getAddress(){
        return this.getStr("address");
    }
        
    public Site setContact(String contact){
        return this.set("contact",contact);
    }

    public String getContact(){
        return this.getStr("contact");
    }
        
    public Site setContactPhone(String contactPhone){
        return this.set("contact_phone",contactPhone);
    }

    public String getContactPhone(){
        return this.getStr("contact_phone");
    }
        
    public Site setEmail(String email){
        return this.set("email",email);
    }

    public String getEmail(){
        return this.getStr("email");
    }
        
    public Site setDeliveryAddress(String deliveryAddress){
        return this.set("delivery_address",deliveryAddress);
    }

    public String getDeliveryAddress(){
        return this.getStr("delivery_address");
    }
        
    public Site setRemark(String remark){
        return this.set("remark",remark);
    }

    public String getRemark(){
        return this.getStr("remark");
    }
        
    public Site setUserId(Long userId){
        return this.set("user_id",userId);
    }

    public Long getUserId(){
        return this.get("user_id");
    }

    public User getUser(){
        User user = User.dao.findById(getUserId());
        if (user != null){
            user.remove("password");
            user.remove("salt");
        }
        return user;
    }

}